# 第三题：参考第 9 节的综合案例中的学生信息管理，来实现一个自动取款机的存取款模拟效果。要求有登陆和退出、查询余额、取钱，存钱等操作

# 账户信息存储
list_acct = ['zhangsan','lisi','wangwu']
list_acct_info = [
    {'name':'zhangsan', 'psw':'zs123', 'dollar':1000},
    {'name':'lisi', 'psw':'ls123', 'dollar':1000},
    {'name':'wangwu', 'psw':'ww123', 'dollar':1000}
]

# 登录账户
def logIn(name,psw):
    for i in list_acct_info:
        if name == i['name']:
            true_name = i['name']
            true_psw = i['psw']
    if psw == true_psw:
        print('登陆成功')
        return 1
    else:
        print('密码错误')

# 创建账户
def create_acc(name,psw):
    temp_dict = {}
    temp_dict['name'] = name
    temp_dict['psw'] = psw
    temp_dict['dollar'] = 0
    list_acct.append(name)
    list_acct_info.append(temp_dict)
    print('创建成功')

# 退出账户
def logOut():
    print('退出登录')

# 查询余额
def check_bal(name):
    for i in list_acct_info:
        if name == i['name']:
            true_dollar = i['dollar']
            print('当前账户余额为：', true_dollar)

# 存钱
def add_bal(name, amount):
    for i in list_acct_info:
        if name == i['name']:
            i['dollar'] = i['dollar'] + amount
            print('存款成功，当前账户余额为', i['dollar'])

# 取钱
def minu_bal(name, amount):
    for i in list_acct_info:
        if name == i['name']:
            true_dollar = i['dollar']
            if true_dollar < amount:
                print('取款失败，当前账户余额不足')
            else:
                i['dollar'] = i['dollar'] - amount
                print('取款成功，当前账户余额为', i['dollar'])

# 主程序
while True:
    # 输出初始界面
    print('=' * 12, '自动取款机', '=' * 12)
    print('{:1} {:13} {:13} {:15}'.format(' ', '1. 登录','2.开户', '3. 退出'))
    print('=' * 40)

    key1 = input('请选择操作：')
    if key1 == '1':
        temp_name = input('请输入账户名：')
        if temp_name in list_acct:
            temp_psw = input('请输入密码：')
            # 变量va接收登录函数返回值
            va = logIn(temp_name, temp_psw)
            # 若登录成功，进入后续操作
            while va:
                print('{:1} {:13} {:15}'.format(' ', '1. 查询余额', '2. 存款'))
                print('{:1} {:13} {:15}'.format(' ', '3. 取款', '4. 退出登录'))
                print('=' * 40)
                key2 = input('请选择操作：')
                if key2 == '1':
                    check_bal(temp_name)
                elif key2 == '2':
                    temp_amount = input('请输入存款金额：')
                    add_bal(temp_name,int(temp_amount))
                elif key2 == '3':
                    temp_amount = input('请输入取款金额：')
                    minu_bal(temp_name, int(temp_amount))
                elif key2 == '4':
                    logOut()
                    break
                else:
                    print('输入有误，请重新输入')
        else:
            print('账户不存在')

    elif key1 == '2':
        temp_name = input('请输入账户名：')
        if temp_name in list_acct:
            print('账户已存在')
        else:
            temp_psw = input('请输入密码：')
            create_acc(temp_name,temp_psw)
    elif key1 == '3':
        print('退出系统')
        break
    else:
        print('输入有误，请重新输入')